package com.example.gaojt;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by gaojt on 9/26/17.
 */

public class VidmateData {

    private static final String TAG = "VidmateData";
    private static final String CRYPTOR_KEY = "rz18efAXUbdiaO7k"; // = "f@0$e^e7&8d*ca-c";
    private static final String IV = "rz18efAXUbdiaO7k"; // = "3646288361054287";
    private static final String ALGORITHM = "AES/CBC/NoPadding"; // = "AES/CBC/NoPadding";
    private static final String CHAR_SET = "utf-8";

    public static void aa(String content) {
        String a = decodeString(content);
        Log.d(TAG, "aa: a = " + a);
        File fileDir = new File(Environment.getExternalStorageDirectory(), "vidmate_data");
        fileDir.mkdirs();
        File file = new File(fileDir, System.currentTimeMillis() + "");
        saveToFile(file,a);
        Log.d(TAG, "aa: ok");
    }

    public static String decodeString(String encodedText) {
        if (!TextUtils.isEmpty(encodedText)) {
            try {
                return new String(decrypt(Base64.decode(encodedText, Base64.DEFAULT), CRYPTOR_KEY));
            } catch (Exception e) {}
        }

        return "";
    }


    private static byte[] decrypt(byte[] encrypted, String key) throws Exception {
        if (encrypted == null || encrypted.length == 0) {
            return null;
        }

        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);

            byte[] keybytes = key.getBytes(CHAR_SET);
            SecretKeySpec keyspec = new SecretKeySpec(keybytes, "AES");
            IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes(CHAR_SET));

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted);
            return original;
        } catch (Exception e) {
            Log.w(TAG, "decrypt: ", e);
            return null;
        }
    }

    public static void saveToFile(File file, String content) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(content);

        } catch (IOException e) {} finally {
            try {
                if (writer != null) writer.close();
            } catch (IOException e) {}
        }

    }

    public static String a(String paramString) {
        try {
            Cipher localCipher = Cipher.getInstance("AES/CBC/NoPadding");
            int i = localCipher.getBlockSize();
            byte[] arrayOfByte1 = paramString.getBytes();
            int j = arrayOfByte1.length;
            if (j % i != 0) {
                j += i - j % i;
            }
            byte[] arrayOfByte2 = new byte[j];
            System.arraycopy(arrayOfByte1, 0, arrayOfByte2, 0, arrayOfByte1.length);
            localCipher.init(1, new SecretKeySpec("rz18efAXUbdiaO7k".getBytes(), "AES"),
                    new IvParameterSpec("rz18efAXUbdiaO7k".getBytes()));
            String str = Base64.encodeToString(localCipher.doFinal(arrayOfByte2), 0);
            return str;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return null;
    }

    public static String b(String paramString) {
        int i = 0;
        for (;;) {
            int k;
            try {
                byte[] arrayOfByte1 = Base64.decode(paramString, 0);
                Cipher localCipher = Cipher.getInstance("AES/CBC/NoPadding");
                localCipher.init(2, new SecretKeySpec("rz18efAXUbdiaO7k".getBytes(), "AES"),
                        new IvParameterSpec("rz18efAXUbdiaO7k".getBytes()));
                byte[] arrayOfByte2 = localCipher.doFinal(arrayOfByte1);
                int j = arrayOfByte2.length;
                k = 0;
                if ((i >= j) || (arrayOfByte2[i] == 0)) {
                    String str = new String(arrayOfByte2, 0, k);
                    return str;
                }
            } catch (Exception localException) {
                localException.printStackTrace();
                return null;
            }
            k++;
            i++;
        }
    }
}
